from django.shortcuts import render
<<<<<<< HEAD
from rest_framework.viewsets import ModelViewSet
from rest_framework.views import APIView
from rest_framework.response import  Response
from django.conf import settings
import os
from .models import Brand
from .serializers import BrandSer
# Create your views here.

=======
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.viewsets import ModelViewSet
from rest_framework.generics import ListCreateAPIView, ListAPIView
from rest_framework.generics import RetrieveUpdateDestroyAPIView
from .models import *
from .serializers import *
from django.conf import settings
import os

# Create your views here.
>>>>>>> f03f6fc2a5e389c57682ccb2441daf9bc7b6dbf9

# 品牌的视图集
class BrandViewSet(ModelViewSet):
    queryset = Brand.objects.all()
    serializer_class = BrandSer
<<<<<<< HEAD
    lookup_field = 'pk'
    lookup_url_kwarg = 'pk'


# 上传图片的视图
class UploadLogoAPIView(APIView):

    # 前端el-upload  自己发送的post 请求
    def post(self, request):

        # 获取上传的数据
        file = request.FILES.get("file")
        print("前端上传的文件:", file, type(file))

        # file 身上有两个属性需要使用
        # file.file   当前的文件字节流 BytesIO
        # file.name   当前的文件的名字

        # 将文件字节流写入服务器
        filepath = os.path.join(settings.STATICFILES_DIRS[0], "images/logos/")
        print("静态文件路径：", filepath)

        filename = filepath + file.name

=======
    lookup_field = "pk"
    lookup_url_kwarg = "pk"


# 上传一个品牌的logo
class UploadLogoView(APIView):
    def post(self, request):
        # print("上传：", dir(request))
        # 获取上传的文件对象
        file = request.FILES.get("file")
        print("上传的文件:", file, type(file))
        # 文件的字节流
        print("文件流：", file.file, file.name)

        # 保存到 /static/images/logos/
        file_dir = os.path.join(settings.STATICFILES_DIRS[0], "images/logos/")
        filename = file_dir + file.name
        # 写入字节流
>>>>>>> f03f6fc2a5e389c57682ccb2441daf9bc7b6dbf9
        with open(filename, "wb") as f:
            f.write(file.file.read())

        return Response({
            "code": "200",
            "msg": "上传成功",
            "file_path": "/static/images/logos/",
            "file_name": file.name
        }, status=200)


<<<<<<< HEAD
# 加载所有的频道数据
from rest_framework.generics import ListAPIView
from .models import *
from .serializers import *


class ListChannelAPIView(ListAPIView):
=======
# 频道组的视图
class ListCreateChannelAPIView(ListCreateAPIView):
>>>>>>> f03f6fc2a5e389c57682ccb2441daf9bc7b6dbf9
    queryset = Group.objects.all()
    serializer_class = GroupSer


<<<<<<< HEAD
# 加载所有的商品类别
class ListCateAPIView(ListAPIView):
=======
# 所有类别的视图
class CateAPIView(ListAPIView):
    # "获取类别信息"
>>>>>>> f03f6fc2a5e389c57682ccb2441daf9bc7b6dbf9
    queryset = Cate.objects.all()
    serializer_class = CateSer


<<<<<<< HEAD
# 加载一个  更新一个  删除一个  频道组数据
from rest_framework.generics import RetrieveUpdateDestroyAPIView


class ChannelAPIView(RetrieveUpdateDestroyAPIView):
    queryset = Group.objects.all()
    serializer_class = GroupSer
    lookup_field = 'pk'
    lookup_url_kwarg = "pk"


# 加载一级类别
class FirstCateAPIView(ListAPIView):
    queryset = Cate.objects.filter(cid=None)
    serializer_class = CateSer


# 增加一个频道组----视图
from rest_framework.views import APIView


class AddChannelView(APIView):

    def post(self, request):
        # 1.接收前端数据
        name = request.data.get("name")
        cate_name = request.data.get("cate_name")
        group_url = request.data.get("group_url")
        sequence = request.data.get("sequence")

        # 2.判断cate_name这个类别是否存在
        cate = Cate.objects.filter(name=cate_name).first()
        if not cate:
            return Response({"code": "400", "msg": "添加的类别不存在"})

        # 3. 组织下数据
        data = {
            "name": name,
            "cate": cate.id,
            "group_url": group_url,
            "sequence": sequence
        }
        # 4. 实例化序列化器对象
        ser = GroupSer(data=data)

        if ser.is_valid():
            ser.save()
            return Response({"code": "200", "msg": "添加频道组成功", "data": ser.data})

        else:
            return Response({"code": "400", "msg": ser.error_messages})


# SKU的视图
class SKUViewSet(ModelViewSet):
    queryset = SKU.objects.all()
    serializer_class = SKUSer
    lookup_field = "pk"
    lookup_url_kwarg = "pk"

# SPU的视图
class SPUViewSet(ModelViewSet):
    queryset = SPU.objects.all()
    serializer_class = SPUSer

=======
# 一级类别的视图
class  FirstCateAPIView(ListAPIView):
    queryset = Cate.objects.all()
    serializer_class = CateSer

    # 重写list方法
    def list(self, request, *args, **kwargs):
        # 获取一级类别
        cates = self.get_queryset().filter(cid=None)
        # 序列化
        cates_ser = self.get_serializer(cates, many=True)

        return Response({
            "code": "200",
            "msg": "获取一级类别成功",
            "cates": cates_ser.data
        })


# 检索一个、更新一个、删除一个频道组数据
class ChannelAPIView(RetrieveUpdateDestroyAPIView):
    """
    获取单个频道  修改单个频道信息  删除单个频道信息
    """
    queryset = Group.objects.all()
    serializer_class = GroupSer
    lookup_field = 'pk'
    lookup_url_kwarg = 'pk'
>>>>>>> f03f6fc2a5e389c57682ccb2441daf9bc7b6dbf9

